package com.nf.health.app.service;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.google.gson.Gson;
import com.nf.health.app.activity.global.MainActivity;
import com.nf.health.app.mydoctor.activity.MyNewDoctorActivity;
import com.nf.health.app.inquiry.activity.MyInquiryActivity;
import com.nf.health.app.models.ChatMessageVo;
import com.nf.health.app.models.PushMessage;
import com.nf.health.app.utils.LogUtil;

import org.greenrobot.eventbus.EventBus;

import cn.jpush.android.api.JPushInterface;

/**
 * 自定义接收器
 * <p/>
 * 如果不定义这个 Receiver，则：
 * 1) 默认用户会打开主界面
 * 2) 接收不到自定义消息
 */
public class MyJPushReceiver extends BroadcastReceiver {
    private static final String TAG = "JPush";
    /**
     * 用药提醒
     */
    private static final String DRUG_WARNING = "1";
    /**
     * 安全提醒
     */
    private static final String SAFETY_WARNING = "2";
    /**
     * 上传数据
     */
    private static final String UPLOAD_DATA = "3";
    /**
     * 问健康
     */
    private static final String ASK_HEALTHY = "4";
    /**
     * 与医生对话
     */
    private static final String TALKING_TO_DOCTOR = "5";
    /**
     * 问诊中医生回复了信息
     */
    private static final String INQUIRY_REPLY = "11";

    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
        LogUtil.e(TAG, "[MyReceiver] onReceive - " + intent.getAction() + ", extras: " + printBundle(bundle));

        if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
            String regId = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);
            LogUtil.e(TAG, "[MyReceiver] 接收Registration Id : " + regId);
            //send the Registration Id to your server...
        } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
            LogUtil.e(TAG, "[MyReceiver] 接收到推送下来的自定义消息: " + bundle.getString(JPushInterface.EXTRA_MESSAGE));
//            String message = bundle.getString(JPushInterface.EXTRA_MESSAGE);
//            ChatMessageVo messageVo = new Gson().fromJson(message, ChatMessageVo.class);
//        	processCustomMessage(context, bundle);
        } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
            LogUtil.e(TAG, "[MyReceiver] 接收到推送下来的通知");
            int notifactionId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);
            LogUtil.d(TAG, "[MyReceiver] 接收到推送下来的通知的ID: " + notifactionId);
            receivingNotification(context, bundle);
        } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
            LogUtil.e(TAG, "[MyReceiver] 用户点击打开了通知");
            //TODO 打开自定义的Activity
            openNotification(context, bundle.getString(JPushInterface.EXTRA_EXTRA));
        } else if (JPushInterface.ACTION_RICHPUSH_CALLBACK.equals(intent.getAction())) {
            LogUtil.e(TAG, "[MyReceiver] 用户收到到RICH PUSH CALLBACK: " + bundle.getString(JPushInterface.EXTRA_EXTRA));
            //在这里根据 JPushInterface.EXTRA_EXTRA 的内容处理代码，比如打开新的Activity， 打开一个网页等..
        } else if (JPushInterface.ACTION_CONNECTION_CHANGE.equals(intent.getAction())) {
            boolean connected = intent.getBooleanExtra(JPushInterface.EXTRA_CONNECTION_CHANGE, false);
            LogUtil.e(TAG, "[MyReceiver]" + intent.getAction() + " connected state change to " + connected);
        } else {
            LogUtil.e(TAG, "[MyReceiver] Unhandled intent - " + intent.getAction());
        }
    }

    private void openNotification(Context context, String msg) {
        PushMessage message = new Gson().fromJson(msg, PushMessage.class);
        if (DRUG_WARNING.equals(message.getType())) {
            Intent i = new Intent(context, MainActivity.class);
            i.putExtra("msg", message.getMsg());
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(i);
        } else if (SAFETY_WARNING.equals(message.getType())) {
            Intent i = new Intent(context, MainActivity.class);
            i.putExtra("msg", message.getMsg());
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(i);
        } else if (UPLOAD_DATA.equals(message.getType())) {
            Intent i = new Intent(context, MainActivity.class);
            i.putExtra("msg", message.getMsg());
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(i);
        } else if (ASK_HEALTHY.equals(message.getType())) {
//			i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP );
//			context.startActivity(i);
        } else if (TALKING_TO_DOCTOR.equals(message.getType())) {
            Intent i = new Intent(context, MyNewDoctorActivity.class);
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
            context.startActivity(i);
        }else if (INQUIRY_REPLY.equals(message.getType())) {
            Intent i = new Intent(context, MyInquiryActivity.class);
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
            context.startActivity(i);
        }
    }

    // 打印所有的 intent extra 数据
    private static String printBundle(Bundle bundle) {
        StringBuilder sb = new StringBuilder();
        for (String key : bundle.keySet()) {
            if (key.equals(JPushInterface.EXTRA_NOTIFICATION_ID)) {
                sb.append("\nkey:" + key + ", value:" + bundle.getInt(key));
            } else if (key.equals(JPushInterface.EXTRA_CONNECTION_CHANGE)) {
                sb.append("\nkey:" + key + ", value:" + bundle.getBoolean(key));
            } else {
                sb.append("\nkey:" + key + ", value:" + bundle.getString(key));
            }
        }
        return sb.toString();
    }

    private void receivingNotification(Context context, Bundle bundle) {
        String title = bundle.getString(JPushInterface.EXTRA_NOTIFICATION_TITLE);

        LogUtil.d(TAG, " title : " + title);
        String message = bundle.getString(JPushInterface.EXTRA_ALERT);
        LogUtil.d(TAG, "message : " + message);
        String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
        LogUtil.d(TAG, "extras : " + extras);

        PushMessage msg = new Gson().fromJson(bundle.getString(JPushInterface.EXTRA_EXTRA), PushMessage.class);
        if (INQUIRY_REPLY.equals(msg.getType())) {
            EventBus.getDefault().post(new ChatMessageVo());//通知InquiryChatActivity更新界面
        }
    }
}
